import 'package:environmental_les_app/common/style/colours.dart';
import 'package:environmental_les_app/common/widgets/bottom_screen_time_dialog.dart';
import 'package:environmental_les_app/common/widgets/scaffold_top_bg.dart';
import 'package:environmental_les_app/common/widgets/top_screen_widget.dart';
import 'package:environmental_les_app/page/main/home/widgets/event_item.dart';
import 'package:flutter_clx_base/common/colours.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';
import 'package:flutter_clx_base/widget/base_list_widget.dart';

import 'index.dart';

class EventListPage extends GetView<EventListController> {
  const EventListPage({super.key});

  // 主视图
  Widget _buildView() {
    return Column(
      children: [
        MyPageAppBar(
          backgroundColor: transparent,
          title: "巡查事件列表",
          actions: [
            InkWell(
              onTap: () {
                Get.bottomSheet(BottomScreenTimeDialog(
                        initialTimes: controller.allTime))
                    .then((value) {
                  logger.d("时间筛选结果: $value");
                  if (value == null) return; // user dismissed
                  if (value is Map) {
                    if (value.isEmpty) {
                      // confirmed but empty -> clear all
                      controller.allTime = {};
                    } else {
                      // merge into existing to preserve other time types
                      controller.allTime = {...controller.allTime, ...value};
                    }
                    controller.listController.reload();
                  }
                });
              },
              child: Container(
                padding:
                    const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
                decoration: const BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(Radius.circular(18)),
                ),
                child: const Row(
                  children: [
                    Icon(Icons.filter_list, color: Colours.textC4C),
                    Text("时间筛选",
                        style: TextStyle(color: Colours.text254, fontSize: 15)),
                  ],
                ),
              ),
            ),
            hGap10,
          ],
        ),
        TabBar(
          tabAlignment: TabAlignment.start,
          isScrollable: true,
          controller: controller.tabController,
          tabs: controller.tabs.map((e) => Tab(height: 33, text: e)).toList(),
          onTap: (index) => controller.onTabClick(index),
        ),
        vGap10,
        // 顶部筛选条（从可复用组件中提取）
        TopScreenWidget(
          filterData: controller.filterData,
          initialSelected: controller.selectedFilters,
          onSelectionChanged: (selected) {
            // selected is Map<String, List<String>>?
            if (selected == null) {
              controller.updateSelection(null);
            } else {
              // controller.updateSelection accepts Map<String, dynamic>? where values may be List
              controller
                  .updateSelection(selected.map((k, v) => MapEntry(k, v)));
            }
            // reload list after selection change
            controller.listController.reload();
          },
        ),
        Expanded(
          child: BaseListWidget(
            canLoadMore: false,
            controller: controller.listController,
            requestData: (page, pageSize, success, error) =>
                controller.getList(page, pageSize, success, error),
            itemBuilder: (context, index, item) => EventItem(item: item),
          ),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<EventListController>(
      init: EventListController(),
      id: "event_list",
      builder: (_) {
        return MyScaffoldTop(child: _buildView());
      },
    );
  }
}
